<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryObj.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="changeValue"
        />
        <!-- 树形组件show-checkbox -->
        <el-tree
          ref="deptTree"
          node-key="id"
          :data="depList"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          @current-change="selectNode"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button
            size="mini"
            type="primary"
            @click="$router.push('/employee/detail')"
          >添加员工</el-button>
          <!-- 1.添加权限-让前端程序员知道
               2.给角色分配权限
               3.分配权限 v-allow="'export-import'"-->
          <el-button size="mini" @click="showExcelDialog=true">excel导入</el-button>

          <!-- {{ $store.state.user.userInfo.roles.points }} -->
          <!-- v-if="$store.state.user.userInfo.roles.points.includes('excel_export')" -->
          <!-- <el-button size="mini" @click="exportEmployee">excel导出</el-button> v-if="$store.state.user.userInfo.roles.points.includes('excel_export')"
           -->
          <el-button

            size="mini"
            @click="exportEmployee"
          >excel导出</el-button>
        </el-row>
        <!-- 表格组件 v-allow="'excel_export-btn'"-->
        <el-table :data="list">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{ row }">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template v-slot="{ row }">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template v-slot="{ row }">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="setCurId(row.id)">角色</el-button>
              <el-button size="mini" type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            layout="sizes,prev, pager, next"
            :background="true"
            :total="total"
            :page-size="queryObj.pagesize"
            :page-sizes="[ 5 , 8, 10,15]"
            :current-page="queryObj.page"
            @current-change="currentChange"
            @size-change="handleSizeChange"
          />
        </el-row>
        <!-- 分页 -->
      </div>
    </div>
    <import-excel :show-excel-dialog.sync="showExcelDialog" @uploadSuccess="loadEmployeeList" />
    <assign-role ref="refRole" :show-role-dialog.sync="showRoleDialog" :cur-id="curId">
      <!--  -->
    </assign-role>
  </div>
</template>

<script>

import FileSaver from 'file-saver'
import { getDepartment } from '@/api/department'
import { getEmployeeList, exportEmployee } from '@/api/employee'
import { arr2tree } from '@/utils/index'
import importExcel from './components/import-excel.vue'
import AssignRole from './components/assignRole.vue'
export default {
  name: 'Employee',
  components: { importExcel, AssignRole },
  data() {
    return {
      showRoleDialog: false,
      showExcelDialog: false,
      depList: [{ name: '传智教育', children: [{ name: '总裁办' }] }], // 组织数据
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      queryObj: {
        page: 1,
        pagesize: 10,
        keyword: '',
        departmentId: ''
      },
      list: [],
      total: 0,
      curId: -1
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    setCurId(id) {
      this.showRoleDialog = true
      this.curId = id
      this.$nextTick(() => {
        this.$refs.refRole.getRoleUser()
      })
    },
    currentChange(page) {
      console.log(page)
      this.queryObj.page = page
      this.loadDepartment()
    },
    handleSizeChange(val) {
      console.log(val)
      this.queryObj.pagesize = val
      this.queryObj.page = 1
      this.loadDepartment()
    },
    async  exportEmployee() {
      const result = await exportEmployee() // 导出所有的员工接口
      // console.log(result) // 使用一个npm包 直接将blob文件下载到本地 file-saver
      // FileSaver.saveAs(blob对象,文件名称)
      FileSaver.saveAs(result, '员工信息表.xlsx') // 下载文件
    },
    changeValue() {
      // 单位时间内只执行最后一次
      // this的实例上赋值了一个timer的属性
      // clearTimeout(this.timer) // 清理上一次的定时器
      // this.timer = setTimeout(() => {
      //   this.queryParams.page = 1
      //   this.getEmployeeList()
      // }, 300)
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryObj.page = 1
        this.loadEmployeeList()
      }, 300)
    },
    // 获取树状图数据
    async loadDepartment() {
    // 递归方法 将列表转化成树形
    // let result = await getDepartment()
      const res = await getDepartment()
      const rs = arr2tree(res.data, 0)
      this.depList = rs
      const id = this.depList[0].id
      this.queryObj.departmentId = id
      this.loadEmployeeList()
      this.$nextTick(() => {
        this.$refs.deptTree.setCurrentKey(id)
      })
    },
    // 发请求获取当前点击的员工数据
    async loadEmployeeList() {
      const res = await getEmployeeList(this.queryObj)
      this.list = res.data.rows
      this.total = res.data.total
      // console.log(this.list)
    },
    // 点击获取树状图id
    selectNode(node) {
      this.queryObj.departmentId = node.id
      this.loadEmployeeList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
